package com.src.xyzk_personal.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.util.Log;

public class Commonfunc {
	private final static String TAG = "Commonfunc";
	/****
	 * @author 把字节数组转换成16进制字符串
	 * @param bArray
	 *            字节数组
	 * @return String 转换成的字符串
	 */
	public static final String bytesToHexString(byte[] bArray, int start,int end) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		// int v_len = bArray.length;
		int last = end - 1;
		//sb.append("0x");
		for (int i = start; i < end; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append("0");
			sb.append(sTemp.toUpperCase());
			if (i != last)
				sb.append(" ");
		}
		return sb.toString();
	}
	public static final String bytesToHexStringP(byte[] bArray, int start,int end) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		int v_len = bArray.length;
		int last = end - 1;
		//sb.append("0x");
		for (int i = start; i < end; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append("0");
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
	public static final String bytesToHexStringP2(byte[] bArray, int start,int end) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		int v_len = bArray.length;
		int last = end - 1;
		//sb.append("0x");
		for (int i = start; i < end; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);

			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
	public static final String byteToHexString(byte bt)
	{//
		String st = Integer.toHexString(0xFF & bt);
		if(st.length() < 2)
			st = "0" + st;
		return st.toUpperCase();
	}
	public static final String byteToString(byte bt)
	{
		String st = Integer.toString(0xFF & bt);
		if(st.length() < 2)
			st = "0" + st;
		return st;
	}
	public static final String msToString(int ms)
	{
		String st = Integer.toString(ms);
		if(st.length() < 2)
			st = "00" + st;
		else if(st.length() < 3)
			st = "0" + st;
		return st;
	}
	public static final String canidToHexString(int canid)
	{
		String st = Integer.toHexString(canid).toUpperCase();
		if(st.length() < 2)
			st = "00" + st;
		else if(st.length() < 3)
			st = "0" + st;
		return st;
	}
	public static byte CalcChechSum(byte[] data, int start, int end) {
		byte chechsum = 0;
		if (start >= 0 && start < end && data != null && data.length > 0) {
			//int calc_len = end - start;
			for (int i = start; i < end; i++) {
				chechsum += data[i];
			}
			return (byte) chechsum;
		}
		return (byte) 0x00;
	}
	public static int HexStringtoInt(String hexstr)
	{
		int x=0,iRet = 0;
		if(hexstr == null)
			return iRet;
		int length = hexstr.length();
		for(int i = 0; i < length; i ++)
		{
			char c = hexstr.charAt(i);
			//对'0'->0，'a'->10
			if(c>='a'&&c<='f'){
				x=c-'a'+10;
			}else if(c>='A'&&c<='F'){
				x=c-'A'+10;
			}else if(c>='0'&&c<='9'){
				x=c-'0';
			}
			iRet=(iRet<<4)| x;//n=n*4+x,移位拼接
		}
		return iRet;
	}
	public static int CmdStringToBytes(String cmdstr,byte[] cmdbuf,int cmdlen)
	{
		if(Common.Debug) Log.i(TAG,"Src:" + cmdstr);
		int i = 0;
		try
		{
			for(i = 0; i < cmdlen; i ++)
			{
				String data = cmdstr.substring(i * 3, i * 3 + 2);
				if(data != null && data.length() == 2)
					cmdbuf[i] = (byte) HexStringtoInt(data);
			}
		}catch (Exception e) {
			// TODO: handle exception
			return i;
		}
		if(Common.Debug) Log.i(TAG,"Obj:"+ bytesToHexString(cmdbuf, 0,cmdlen));
		return cmdlen;
	}

	public static int CheryCmdStringToBytes(String cmdstr,byte[] cmdbuf,int cmdlen)
	{
		for(int i = 0; i < cmdlen/2; i ++)
		{
			String data = cmdstr.substring(i*2,i*2+2);
			cmdbuf[i] = (byte) HexStringtoInt(data);
		}
		return cmdlen;
	}

	public static int StringToBytes(String str,byte[] buf,int cmdlen)
	{
		if(str.length() < cmdlen * 2)
		{
			cmdlen = str.length()  / 2;
		}
		if(Common.Debug) Log.i(TAG,"Src:" + str);
		for(int i = 0; i < cmdlen; i ++)
		{
			String data = str.substring(i * 2, i * 2 + 2);
			buf[i] = (byte) HexStringtoInt(data);
		}
		if(Common.Debug) Log.i(TAG,"Obj:"+ bytesToHexString(buf, 0,cmdlen));
		return cmdlen;
	}
	//字符串转为byte数组
	public static int StringTobyteString(String str,byte[] buf,int cmdlen)
	{
		for(int i = 0; i < cmdlen; i ++)
		{
			buf[i] = (byte) str.charAt(i);
		}
		return cmdlen;
	}
	//整数转ip
	public static String IntToIp(int ip)
	{
		return ( ip & 0xFF) + "." +
				((ip >> 8 ) & 0xFF) + "." +
				((ip >> 16 ) & 0xFF)  + "." +
				((ip >> 24 ) & 0xFF );
	}
	//vin校验获取字符对应值
	public static int GetVinFormat(byte Pv)
	{
		if(Pv >= '0' && Pv <= '9')
			return Pv - 0x30;
		else if(Pv >= 'A' && Pv <= 'H')
			return Pv - 'A' + 1;
		else if(Pv >= 'J' && Pv <= 'N')
			return Pv - 'J' + 1;
		else if(Pv == 'P')
			return 7;
		else if(Pv == 'R')
			return 9;
		else if(Pv >= 'S' && Pv <= 'Z')
			return Pv - 'S' + 2;
		else
			return -1;
	}
	//校验VIN是否合法
	public static boolean CheckVinFormat(byte [] Pvin)
	{
		if(Pvin.length != 17) return false;
		if(Pvin[0] < 'A' || Pvin[0] > 'Z') return false;
		int [] v_jiaquan = {8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2};
		int v_slt = 0;
		for(int i = 0; i < 17; i ++)
		{
			if(i == 8) continue;
			v_slt += GetVinFormat(Pvin[i]) * v_jiaquan[i];
		}
		int v_result = v_slt % 11;
		if(v_result == 10)
		{
			if(Pvin[8] == 'X') return true;
		}
		else
		{
			if(Pvin[8] == (v_result + 0x30)) return true;
		}
		return false;
	}
	//校验配置码是否合法
	public static boolean CheckConfigFormat(String Pvin)
	{

		if(Pvin.length()== 196||Pvin.length()==16||Pvin.length()==64)
		{
			return true;
		}
		return false;
	}
	//十六进制转BCD码
	public static byte HexToBCD(byte code)
	{
		byte bcd = 0;
		bcd = (byte) ((code / 10) * 0x10 + (code % 10));
		return bcd;
	}
	public static byte HexStrToByte(byte[] Pstr,int start)
	{
		byte a = (byte) ((CharToHexInt(Pstr[start]) << 4) + CharToHexInt(Pstr[start + 1]));
		return  a;
	}
	public static int CharToHexInt(byte c)
	{
		int x = 0;
		if(c>='a'&&c<='f'){
			x=c-'a'+10;
		}else if(c>='A'&&c<='F'){
			x=c-'A'+10;
		}else if(c>='0'&&c<='9'){
			x=c-'0';
		}
		return x;
	}

	//获取Dictionary下面的子文件夹
	public static List<String> GetDictionarySonList(String DirName)
	{
		List<String> mylist = new ArrayList<String>();
		File myfile = null;
		try
		{
			myfile = new File(DirName);
			if (!myfile.exists()) {
				myfile.mkdir();
			}
			String collectionPath = myfile.getPath();
			File file = new File(collectionPath);
			File[] tempList = file.listFiles();

			for (int i = 0; i < tempList.length; i++) {
				if (tempList[i].isDirectory()) {
					System.out.println("文     件：" + tempList[i].getName());
					// tempList[i].toString();// 路径
					// tempList[i].getName();// 文件名
					// 文件名
					String fileName = tempList[i].getName();
					mylist.add(fileName);

				}
			}
		}
		catch (Exception e) {
			Log.i("FileError",e.toString());
		}
		return mylist;
	}

	public static void getTextFromS19(String filename,byte[] datas,ArrayList<Long> startAdd,ArrayList<Integer> sizes){
		BufferedReader br;
		String line="";
		int allNum=0;//已读总字节数
		long frontAdd=0;//前一行地址
		long frontSize=0;//前一行有效字节数
		long currentAdd;//当前地址
		int currentLen=0;//当前有效字节数
		int duanLen=0;//每一段总字节数
		boolean first=true;
		try {
			br=new BufferedReader(new FileReader(filename));
			while((line=br.readLine())!=null){
				if(line.length()>1){
					int type=line.charAt(1)-48;
					if(type>0&&type<4){
						currentLen=Integer.valueOf(line.substring(2,4),16)-(type+2);
						currentAdd=Long.valueOf(line.substring(4,6+type*2),16);
						if(((frontAdd+frontSize)==0&&currentAdd==0)||(frontAdd+frontSize!=currentAdd)){
							if(!first){//是否是第一次
								sizes.add(duanLen);
								duanLen=0;
							}
							first=false;
							startAdd.add(currentAdd);
						}
						byte[]e=line.substring(6+type*2,(int)currentLen*2+6+type*2).getBytes();
						for (int m =0; m <currentLen; m++) {
							datas[m+allNum]=(byte) ((CharToHexInt(e[m*2])<<4)|CharToHexInt(e[m*2+1]));
						}
						duanLen+=currentLen;
						frontAdd=currentAdd;
						frontSize=currentLen;
						allNum+=currentLen;
					}
				}
			}
			sizes.add(duanLen);//最后一段
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	public static void getTextFromHex(String filename,byte[] datas,ArrayList<Long> startAdds,ArrayList<Integer> sizes){
		BufferedReader br;
		String line="";
		int allNum=0;//已读总字节数
		long frontAdd=0;//前一行地址
		long frontSize=0;//前一行有效字节数
		long start = 0;//当前地址的前两个字节
		long currentAdd = 0;//当前地址
		int currentLen=0;//当前有效字节数
		int duanLen=0;//每一段总字节数
		boolean invalied=false;//无效字节
		boolean first=true;
		try {
			br=new BufferedReader(new FileReader(filename));
			while((line=br.readLine())!=null){
				if(line.length()<8)continue;
				if(line.charAt(8)==52){//04表示起始地址前两个字节
					start=Long.valueOf(line.substring(9,13)+"0000",16);
					invalied=false;
				}else if(line.charAt(8)==48){//00表示有效数据
					if(invalied) continue;
					currentLen=Integer.valueOf(line.substring(1,3),16);
					currentAdd=start+Long.valueOf(line.substring(3,7),16);
					if(((frontAdd+frontSize)==0&&currentAdd==0)||(frontAdd+frontSize!=currentAdd)){
						if(!first){
							sizes.add(duanLen);//不是首段
							duanLen=0;
						}
						first=false;
						startAdds.add(currentAdd);
					}
					byte[]e=line.substring(9,line.length()-2).getBytes();
					for (int m =0; m <currentLen; m++) {
						datas[m+allNum]=(byte) ((CharToHexInt(e[m*2])<<4)|CharToHexInt(e[m*2+1]));
					}
					duanLen+=currentLen;
					frontAdd=currentAdd;
					frontSize=currentLen;
					allNum+=currentLen;
				}else{
					invalied=true;
				}
			}
			sizes.add(duanLen);//最后一段
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public static byte[] IntTobytes(long num){
		byte[] b=new byte[4];
		b[0]=(byte) (num>>24);
		b[1]=(byte) (num>>16);
		b[2]=(byte) (num>>8);
		b[3]=(byte) num;
		return b;
	}

	public static void CalcCRC(byte[] data,int datasize,byte[] checksum){

		int resul_crc=0;
		int crc =0xffff;
		int temp=0;
		int i=0;
		int[] crctab ={ 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
				0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
				0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
				0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
				0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
				0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
				0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
				0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
				0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
				0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
				0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
				0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
				0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
				0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
				0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
				0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
				0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
				0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
				0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
				0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
				0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
				0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
				0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
				0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
				0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
				0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
				0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
				0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
				0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
				0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
				0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
				0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0};
		for ( i = 0; i < datasize; i++)
		{
			temp=((crc>>8)^data[i])&0xff;

			crc=((crc<<8)^crctab[temp])&0xFFFF;
		}
		resul_crc=crc;
		checksum[0]=(byte) ((byte) (resul_crc>>8)&0xff);
		checksum[1]=(byte) ((byte) (resul_crc&0xff));
	}
	public static int bytesCompare(byte [] A,byte [] B)
	{
		int isok = 1;
		for(int i = 0; i < A.length; i ++)
		{
			if(A[i] != B[i])
			{
				isok = 0;
				break;
			}
		}
		return isok;
	}

	//获取Dictionary下面的子文件
	public static HashMap<Integer,String> getFlashFileSonList(String DirName)
	{
		HashMap<Integer, String> map=new HashMap<Integer, String>();
		File mainfile = new File(DirName);
		if(!mainfile.isDirectory()) return map;
		File [] files = mainfile.listFiles();
		if(files.length <= 0) return map;
		for(int i = 0; i < files.length; i ++){
			String name=files[i].getName();
			if(name.toUpperCase().contains("DRIVER")){
				map.put(0, name);
			}else{
				map.put(1, name);
			}
		}
		return map;
	}
	//返回读到的多段数据的总长度,Padr[0] -- 表示地址组个数,4byte一组
	public static int GetS19FileLength(String Pfile,byte [] Padr) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		InputStreamReader inputreader = new InputStreamReader(fileInputStream);
		BufferedReader buffreader = new BufferedReader(inputreader);
		String line = "";
		long len = 0;
		long v_adress = 0;
		long v_blockadress = 0;
		long v_block = 0;
		long v_blocklen = 0;
		boolean isstart = false;
		byte [] v_adr = new byte[4];
		//byte [] v_buf = new byte[64];
		while ((line = buffreader.readLine()) != null)
		{
			//解析数据
			if(line == null) break;
			if(line.length() > 0)
			{
				v_adr.clone();
				len = ReadS19Line(line, v_adr,null);
				v_adress = (v_adr[0]&0xFF) * 0x1000000 + (v_adr[1]&0xFF) * 0x10000 + (v_adr[2]&0xFF) * 0x100 + (v_adr[3]&0xFF);
				if(len > 0)  //开始
				{
					if(isstart == false || ((v_blockadress + v_blocklen) != v_adress)) //开启一个新的块
					{
						isstart = true;
						//拷贝起始地址
						v_blockadress = v_adress;
						v_blocklen = 0;
						System.arraycopy(v_adr, 0, Padr, (int) (1 + v_block * 4), 4);
						v_block ++;
					}
					v_blocklen += len;
					readlen += len;
				}
				else if(len == -1) //开始
				{

				}
				else
				{
					break;
				}

			}
		}
		if(v_block < 255) //正常
			Padr[0] = (byte) v_block;
		else
			readlen = 0; //异常
		return readlen;
	}
	//返回总长度，Pblock[0] -- 块数，后面是每块长度
	public static int ReadS19File(String Pfile,byte [] readbuf,int Pbufadr,int [] Pblock) throws IOException
	{
		int readlen = Pbufadr;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		InputStreamReader inputreader = new InputStreamReader(fileInputStream);
		BufferedReader buffreader = new BufferedReader(inputreader);
		String line = "";
		long len = 0;
		long v_adress = 0;
		long v_blockadress = 0;
		long v_block = 0;
		long v_blocklen = 0;
		long v_buf_size = readbuf.length;
		byte [] v_adr = new byte[4];
		byte [] v_buf = new byte[64];
		while ((line = buffreader.readLine()) != null)
		{
			//解析数据
			if(line == null) break;
			if(readlen >= readbuf.length) break;
			if(line.length() > 0)
			{
				v_adr.clone();
				len = ReadS19Line(line, v_adr, v_buf);
				v_adress = (v_adr[0]&0xFF) * 0x1000000 + (v_adr[1]&0xFF) * 0x10000 + (v_adr[2]&0xFF) * 0x100 + (v_adr[3]&0xFF);
				if(len > 0)  //开始
				{
					if(((v_blockadress + v_blocklen) != v_adress) || (v_adress == 0)) //开启一个新的块
					{
						//拷贝起始地址
						//System.arraycopy(v_adr, 0, Padr, 1 + v_block * 4, 4);
						if(v_blocklen > 0)
							Pblock[(int) v_block] = (int) v_blocklen;
						v_blocklen = 0;
						v_blockadress = v_adress;
						v_block ++;
					}
					if(v_buf_size < (readlen + len))
						break;
					System.arraycopy(v_buf, 0, readbuf, readlen, (int) len);
					v_blocklen += len;
					readlen += len;
				}
				else if(len == -1) //开始
				{

				}
				else
				{
					break;
				}
			}
		}
		//记录最后一块
		if(v_blocklen > 0)
			Pblock[(int) v_block] = (int) v_blocklen;
		//记录块数
		Pblock[0] = (int) v_block;
		return readlen;
	}
	static int ReadS19Line(String Pline,byte [] Padr, byte [] Pbuf)
	{
		int len = 0;
		String head = Pline.substring(0,2);
		Pline = Pline.substring(2);
		byte [] v_buf = new byte[Pline.length() / 2];
		if(head.equals("S3") || head.equals("S2") || head.equals("S1"))
		{
			len = Commonfunc.StringToBytes(Pline, v_buf, v_buf.length);
			if(len < 5 || (len != ((v_buf[0]&0xFF) + 1))) return 0;
			//addr
			int adrlen = (head.getBytes()[1]&0xFF) - 0x30 + 1;
			System.arraycopy(v_buf, 1, Padr, 4 - adrlen, adrlen);
			len = len - adrlen - 2;
			if(Pbuf != null)
				System.arraycopy(v_buf, adrlen + 1, Pbuf, 0, len);
		}
		else if(head.equals("S0")) //开始获取地址高字节
		{
			len = -1;
		}
		else if(head.equals("S7") || head.equals("S8")) //结束
		{
			len = -2;
		}
		return len;
	}
	//按S19多段解析存储,Pstart--起始地址
	public static int GetHexFileLength(String Pfile,byte [] Padr,Long Pstart) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		InputStreamReader inputreader = new InputStreamReader(fileInputStream);
		BufferedReader buffreader = new BufferedReader(inputreader);
		String line = "";
		int len = 0;

		long sk = (Pstart&0xFFFFFFFF);
		//int v_adress = 0;
		boolean isstart = false;
		byte [] v_adr = new byte[4];
		//byte [] v_buf = new byte[64];
		while ((line = buffreader.readLine()) != null)
		{
			//解析数据
			if(line == null) break;
			if(line.length() > 0)
			{
				v_adr.clone();
				len = ReadHexLine(line, v_adr,null);
				//v_adress = (v_adr[0]&0xFF) * 0x1000000 + (v_adr[1]&0xFF) * 0x10000 + (v_adr[2]&0xFF) * 0x100 + (v_adr[3]&0xFF);
				if(isstart == false)
				{
					if(len == 1000) //头
					{
						//copy
						System.arraycopy(v_adr, 0, Padr, 0, 2);
						Padr[0] = 0x01;
						Padr[1] = v_adr[0];
						Padr[2] = v_adr[1];
						if(Pstart > 0)
						{
							if((((Padr[1]&0xFF) << 8) + ((Padr[2]&0xFF) << 0)) < Pstart)
								continue;
						}
						readlen = 0;
						isstart = true;
					}
					else if(len == 0)
					{
						break;
					}
				}
				else if(len > 0 && len < 100)
				{
					if(readlen == 0)
						System.arraycopy(v_adr, 0, Padr, 3, 2);
					readlen += len;
				}
				else if(len == 1000)
				{

				}
				else
				{
					break;
				}
			}
		}
		buffreader.close();
		return readlen;
	}
	//返回 0--结束,1000-头地址,1- 100实际数据长度
	static int ReadHexLine(String Pline,byte [] Padr,byte [] Pdata)
	{
		int error = 0;
		int len = 0;
		String head = Pline.substring(0,3);
		Pline = Pline.substring(3);
		byte [] v_buf = new byte[64];
		if(head.equals(":02")) //头
		{
			len = Commonfunc.StringToBytes(Pline, v_buf, v_buf.length);
			if(len < 4) return 0;
			System.arraycopy(v_buf, 3, Padr, 0,2);
			error = 1000;
		}
		else if(head.equals(":00")) //结束
		{
			error = 0;
		}
		else
		{
			int buflen = HexStringtoInt(head.substring(1));
			len = Commonfunc.StringToBytes(Pline, v_buf, v_buf.length);
			if(len < 4) return 0;
			if((buflen + 4) == len) //ok
			{
				error = buflen;
				//copy
				System.arraycopy(v_buf, 0, Padr, 0,2);  //address
				if(Pdata != null)
					System.arraycopy(v_buf, 3, Pdata, 0,buflen);	//data
			}
			else
				error = 0;
		}
		return error;
	}
	//返回总长度，Pblock[0] -- 块数，后面是每块长度
	public static int ReadHexFile(String Pfile,byte [] readbuf,int [] Pblock,Long Pstart) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		InputStreamReader inputreader = new InputStreamReader(fileInputStream);
		BufferedReader buffreader = new BufferedReader(inputreader);
		String line = "";
		int len = 0;
		//int v_adress = 0;
		boolean isstart = false;
		byte [] v_adr = new byte[4];
		byte [] v_buf = new byte[64];
		while ((line = buffreader.readLine()) != null)
		{
			//解析数据
			if(line == null) break;
			if(line.length() > 0)
			{
				v_adr.clone();
				len = ReadHexLine(line, v_adr,v_buf);
				//v_adress = (v_adr[0]&0xFF) * 0x1000000 + (v_adr[1]&0xFF) * 0x10000 + (v_adr[2]&0xFF) * 0x100 + (v_adr[3]&0xFF);
				if(isstart == false)
				{
					if(len == 1000) //头
					{

						//copy
						//System.arraycopy(v_adr, 0, Padr, 0, 2);
						//Padr[0] = v_adr[0];
						//Padr[1] = v_adr[1];
						if(Pstart > 0)
						{
							if((((v_adr[0]&0xFF) << 8) + ((v_adr[1]&0xFF) << 0)) < Pstart)
								continue;
						}
						readlen = 0;
						isstart = true;
					}
					else if(len == 0)
					{
						break;
					}
				}
				else if(len > 0 && len < 100)
				{
					System.arraycopy(v_buf, 0, readbuf, readlen, len);
					readlen += len;
				}
				else if(len == 1000)
				{

				}
				else
				{
					break;
				}
			}
		}
		buffreader.close();
		//临时先用
		Pblock[0] = 1;
		Pblock[1] = readlen;
		return readlen;
	}
	public static int GetBinFileLength(String Pfile) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		int len = 0;
		byte [] v_buf = new byte[16];
		len = fileInputStream.read(v_buf);
		while (len > 0)
		{
			//数据
			readlen += len;
			len = fileInputStream.read(v_buf);
		}
		fileInputStream.close();
		return readlen;
	}
	public static int ReadBinFile(String Pfile,byte [] readbuf) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		int len = 0;
		byte [] v_buf = new byte[16];
		len = fileInputStream.read(v_buf);
		while (len > 0)
		{
			//copy数据
			System.arraycopy(v_buf, 0, readbuf, readlen, len);
			readlen += len;
			len = fileInputStream.read(v_buf);
		}
		fileInputStream.close();
		return readlen;
	}
	public static int ReadBinFile_TsingShan(String Pfile,byte [] readbuf) throws IOException
	{
		int readlen = 0;
		FileInputStream fileInputStream = new FileInputStream(Pfile);
		int len = 0;
		byte [] v_buf = new byte[16];
		byte [] v_adr = new byte[4];
		len = fileInputStream.read(v_adr);
		len = fileInputStream.read(v_buf);
		while (len > 0)
		{
			//copy数据
			System.arraycopy(v_buf, 0, readbuf, readlen, len);
			readlen += len;
			len = fileInputStream.read(v_buf);
		}
		fileInputStream.close();
		return readlen;
	}
	/**
	 * 获取日期
	 * @param type  "-"或""
	 * @return 返回日期
	 */
	public static String getDate(String type){
		return new SimpleDateFormat("yyyy"+type+"MM"+type+"dd",Locale.CHINA).format(new Date());
	}

	/**
	 * 获取时间 时间格式 24时制
	 * @param type  ":"或""
	 * @return 返回时间
	 */
	public static String getTime(String type){//24时制,type="",:;
		return new SimpleDateFormat("HH"+type+"mm"+type+"ss",Locale.CHINA).format(new Date());
	}

	/**
	 * 获取日期时间 时间格式 24时制
	 * @return 返回时间
	 */
	public static String getDateTime(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA).format(new Date());
	}
	/**
	 * 获取日期时间毫秒  时间格式 24时制
	 * @return 返回时间
	 */
	public static String getTimeMs(){
		Date time=new Date();
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA).format(time)+"."+String.valueOf(time.getTime()%1000+1000).substring(1);
	}
	/**
	 * 计算校验码
	 * @param data数组
	 * @param start起始位置
	 * @param end 结束位置
	 * @return 校验码byte
	 */
	public static byte calcuCheckCode(byte[] data, int start, int end) {
		byte chechsum = 0;
		if (start >= 0 && start < end && data != null && data.length > 0) {
			for (int i = start; i < end; i++) {
				chechsum += data[i];
			}
			return (byte) chechsum;
		}
		return 0;
	}
	static short [] crctab=
			{
					0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
					(short) 0x8108, (short) 0x9129, (short) 0xA14A, (short) 0xB16B, (short) 0xC18C, (short) 0xD1AD, (short) 0xE1CE, (short) 0xF1EF,
					0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
					(short) 0x9339, (short) 0x8318, (short) 0xB37B, (short) 0xA35A, (short) 0xD3BD, (short) 0xC39C, (short) 0xF3FF, (short) 0xE3DE,
					0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
					(short) 0xA56A, (short) 0xB54B, (short) 0x8528, (short) 0x9509, (short) 0xE5EE, (short) 0xF5CF, (short) 0xC5AC, (short) 0xD58D,
					0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
					(short) 0xB75B, (short) 0xA77A, (short) 0x9719, (short) 0x8738, (short) 0xF7DF, (short) 0xE7FE, (short) 0xD79D, (short) 0xC7BC,
					0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
					(short) 0xC9CC, (short) 0xD9ED, (short) 0xE98E, (short) 0xF9AF, (short) 0x8948, (short) 0x9969, (short) 0xA90A, (short) 0xB92B,
					0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
					(short) 0xDBFD, (short) 0xCBDC, (short) 0xFBBF, (short) 0xEB9E, (short) 0x9B79, (short) 0x8B58, (short) 0xBB3B, (short) 0xAB1A,
					0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
					(short) 0xEDAE, (short) 0xFD8F, (short) 0xCDEC, (short) 0xDDCD, (short) 0xAD2A, (short) 0xBD0B, (short) 0x8D68, (short) 0x9D49,
					0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
					(short) 0xFF9F, (short) 0xEFBE, (short) 0xDFDD, (short) 0xCFFC, (short) 0xBF1B, (short) 0xAF3A, (short) 0x9F59, (short) 0x8F78,
					(short) 0x9188, (short) 0x81A9, (short) 0xB1CA, (short) 0xA1EB, (short) 0xD10C, (short) 0xC12D, (short) 0xF14E, (short) 0xE16F,
					0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
					(short) 0x83B9, (short) 0x9398, (short) 0xA3FB, (short) 0xB3DA, (short) 0xC33D, (short) 0xD31C, (short) 0xE37F, (short) 0xF35E,
					0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
					(short) 0xB5EA, (short) 0xA5CB, (short) 0x95A8, (short) 0x8589, (short) 0xF56E, (short) 0xE54F, (short) 0xD52C, (short) 0xC50D,
					0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
					(short) 0xA7DB, (short) 0xB7FA, (short) 0x8799, (short) 0x97B8, (short) 0xE75F, (short) 0xF77E, (short) 0xC71D, (short) 0xD73C,
					0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
					(short) 0xD94C, (short) 0xC96D, (short) 0xF90E, (short) 0xE92F, (short) 0x99C8, (short) 0x89E9, (short) 0xB98A, (short) 0xA9AB,
					0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
					(short) 0xCB7D, (short) 0xDB5C, (short) 0xEB3F, (short) 0xFB1E, (short) 0x8BF9, (short) 0x9BD8, (short) 0xABBB, (short) 0xBB9A,
					0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
					(short) 0xFD2E, (short) 0xED0F, (short) 0xDD6C, (short) 0xCD4D, (short) 0xBDAA, (short) 0xAD8B, (short) 0x9DE8, (short) 0x8DC9,
					0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
					(short) 0xEF1F, (short) 0xFF3E, (short) 0xCF5D, (short) 0xDF7C, (short) 0xAF9B, (short) 0xBFBA, (short) 0x8FD9, (short) 0x9FF8,
					0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
			};
	public static short me1781_CalcCRC16(byte [] data,int Pstart,int Plen,short Pcrc)
	{
		short crc=(short) 0xffff; /* initial value */
		if(Pcrc != crc)
			crc = Pcrc;
		byte tmp;
		int i;
		for(i=0;i<Plen;i++) {
			tmp=(byte) ((crc>>8)^data[Pstart + i]);
			crc=(short) ((crc<<8)^crctab[tmp&0xFF]);
		}
		return crc;
	}
	public static short JX65_6DCT_Carc(byte [] data,int Pstart,int Plen,short Pcrc)
	{
		short crc=(short) 0xffff; /* initial value */
		if(Pcrc != crc)
			crc = Pcrc;
		byte tmp;
		int i;
		for(i=0;i<Plen;i++) {
			tmp=(byte) ((crc>>8)^data[Pstart + i]);
			crc=(short) ((crc<<8)^crctab[tmp&0xFF]);
		}
		return crc;
	}
}
